package dbdao;

import com.lanou.horo.myapplication.dbdao.DaoSession;
import com.lanou.horo.myapplication.dbdao.FristDbEntity;
import com.lanou.horo.myapplication.dbdao.FristDbEntityDao;
import com.lanou.horo.myapplication.dbdao.HostDbEntity;
import com.lanou.horo.myapplication.dbdao.HostDbEntityDao;

import java.util.List;

import application.BasisApplication;
import de.greenrobot.dao.query.DeleteQuery;
import de.greenrobot.dao.query.QueryBuilder;

/**
 * Created by dllo on 15/11/28.
 */
public class FristDBHelper {
    private static  FristDBHelper helper;
    private static DaoSession daoSession;
    private FristDBHelper(){

    }
    public static FristDBHelper getInstance() {
        if (helper == null) {
            helper = new FristDBHelper();
            daoSession = BasisApplication.getDaoSession();//初始化
        }
        return helper;
    }
    /**
     * 往数据库中添加集合对象
     *
     * @param themes
     */
    public void saveHost(List<FristDbEntity> re) {
        // TODO Auto-generated method stub
        daoSession.getFristDbEntityDao().insertInTx(re);//先获得表的DAO
        //daoSession数据库对象 管理都有表 所以用daoSession中get
    }

    public void addTo( FristDbEntity item ) {
        daoSession.getFristDbEntityDao().insert(item);

    }
    /**
     * 更新方法
     * @param t
     */
    public void updataHost(FristDbEntity t) {
        // TODO Auto-generated method stub

        daoSession.getFristDbEntityDao().update(t);
    }

    /**
     * 查询数据库中表中的所有数据
     * @return
     */
    public List<FristDbEntity> queryAll() {
        return daoSession.getFristDbEntityDao().loadAll();//loadAll()返回所有数据
        //daoSession.getThemesDao() 相找到了ThemesDao()
    }
    /**
     * 删除
     * @param id
     */
    public void deleById(long id) {
        QueryBuilder<FristDbEntity> qb = daoSession.getFristDbEntityDao().queryBuilder();
        DeleteQuery<FristDbEntity> db = qb.where(FristDbEntityDao.Properties.Id.eq(id)).buildDelete();
        db.executeDeleteWithoutDetachingEntities();

    }
    /**
     * 按照id查询
     * @param id
     * @return
     */
    public List<FristDbEntity> querById(long id) {
        QueryBuilder<FristDbEntity> db = daoSession.getFristDbEntityDao().
                queryBuilder();//获得表的
        db.where(FristDbEntityDao.Properties.Id.eq(id));//Properties对应表的属性  id等于传过来的id值
        return db.list();

    }
}
